﻿using Microsoft.AspNetCore.Mvc;
using SHRIS.MIS.Business.SystemManage;
using SHRIS.MIS.Common.Model;
using SHRIS.MIS.Entity.SystemManage;
using SHRIS.MIS.IService.SystemManage;
using SHRIS.MIS.Model.Param.SystemManage;
using SHRIS.MIS.Model.Result;
using SHRIS.MIS.Service.Cache;
using SHRIS.MIS.Web.Controllers;
using SHRIS.MIS.Web.Filter;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace SHRIS.MIS.Web.Areas.SystemManage.Controllers
{
    [Area("SystemManage")]
    public class MenuController : BaseController
    {
        #region 服务注入
        private MenuBLL _menuBLL;

        private IMenuService _menuService;
        private MenuCache _menuCache;

        public MenuController(IMenuService menuService,MenuBLL menuBLL, MenuCache menuCache)
        {
            _menuService = menuService;
            _menuBLL = menuBLL;
            _menuCache = menuCache;
        }
        #endregion

        #region 视图功能
        [AuthorizeFilter("system:menu:view")]
        public IActionResult MenuIndex()
        {
            return View();
        }

        public IActionResult MenuForm()
        {
            return View();
        }

        public IActionResult MenuChoose()
        {
            return View();
        }
        public IActionResult MenuIcon()
        {
            return View();
        }
        #endregion

        #region 获取数据
        [HttpGet]
        [AuthorizeFilter("system:menu:search,system:role:search")]
        public async Task<IActionResult> GetListJson(MenuListParam param)
        {
            var obj = new TEntity<List<MenuEntity>>();

            List<MenuEntity> list = await _menuService.GetList(param);
            obj.Data = list;
            obj.Tag = 1;
            return Json(obj);
        }

        [HttpGet]
        [AuthorizeFilter("system:menu:search,system:role:search")]
        public async Task<IActionResult> GetMenuTreeListJson(MenuListParam param)
        {
            var obj = new TEntity<List<ZtreeInfo>>();
            obj.Data = new List<ZtreeInfo>();

            List<MenuEntity> list = await _menuService.GetList(param);

            foreach (MenuEntity menu in list)
            {
                obj.Data.Add(new ZtreeInfo
                {
                    id = menu.Id,
                    pId = menu.ParentId,
                    name = menu.MenuName
                });
            }

            obj.Tag = 1;
            return Json(obj);
        }

        [HttpGet]
        public async Task<IActionResult> GetFormJson(Guid id)
        {
            TEntity<MenuEntity> obj = await _menuBLL.GetEntity(id);
            return Json(obj);
        }

        [HttpGet]
        public async Task<IActionResult> GetMaxSortJson(Guid parentId)
        {
            TEntity<int> obj = await _menuBLL.GetMaxSort(parentId);
            return Json(obj);
        }
        #endregion

        #region 提交数据
        [HttpPost]
        [AuthorizeFilter("system:menu:add,system:menu:edit")]
        public async Task<IActionResult> SaveFormJson(MenuEntity entity)
        {
            TEntity<string> obj = await _menuBLL.SaveForm(entity);
            return Json(obj);
        }

        [HttpPost]
        [AuthorizeFilter("system:menu:delete")]
        public async Task<IActionResult> DeleteFormJson(string ids)
        {
            TEntity obj = await _menuBLL.DeleteForm(ids);
            return Json(obj);
        }
        #endregion
    }
}